package com.example.shopping1902.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyboardShortcutGroup;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.shopping1902.R;

import java.util.List;

public class BuyDialog extends Dialog implements View.OnClickListener {
    ImageView iv;
    AmountView amountView;
    Button ensureBtn;
    public interface OnEnsureListener{
        public void onEnsure(int count);
    }

    OnEnsureListener onEnsureListener;
    public void setOnEnsureListener(OnEnsureListener onEnsureListener){
        this.onEnsureListener = onEnsureListener;
    }
    public BuyDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_amount);
        iv = findViewById(R.id.dialog_amount_iv);
        amountView = findViewById(R.id.dialog_amountview);
        ensureBtn = findViewById(R.id.dialog_amount_btn);
        iv.setOnClickListener(this);
        ensureBtn.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_amount_iv:
                cancel();
                break;
            case R.id.dialog_amount_btn:
                if (onEnsureListener!=null) {
                    int count = amountView.getAmountCount();
                    onEnsureListener.onEnsure(count);
                }
                break;
        }
    }
    public void setDialogWidth(){
        Window window = getWindow();//当前屏幕窗口对象
        WindowManager.LayoutParams wlp = window.getAttributes();//获取窗口信息参数
        Display d = window.getWindowManager().getDefaultDisplay();
        wlp.width = (int)d.getWidth();//对话框的窗口宽度为屏幕窗口宽度
        wlp.gravity = Gravity.BOTTOM;//从底部弹出对话框
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(wlp);
        //自动弹出软键盘

    }
}